//
// Description: 27. 移除元素
// Created by Loading on 2024/3/30.
//

#include <bits/stdc++.h>

using namespace std;

int removeElement(vector<int> &nums, int val) {
    if (nums.empty()) {
        return 0;
    }
    int i = 0, j = nums.size() - 1;
    for (; i < j;) {
        if (nums[i] != val) {
            ++i;
            continue;
        }
        if (nums[j] == val) {
            --j;
            continue;
        }
        nums[i++] = nums[j--];
    }
    if (i == j && nums[i] != val) {
        return i + 1;
    }
    return i;
}

int main() {
    vector<int> v{3, 2, 2, 3};
    int val = 3;
    int res = removeElement(v, val);
    cout << res << endl;
    for (int i = 0; i < res; ++i) {
        cout << v[i] << ' ';
    }
    cout << endl;

    return 0;
}